define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bdl/bd/wxuser/index' + location.search,
                    add_url: 'bdl/bd/wxuser/add',
                    edit_url: 'bdl/bd/wxuser/edit',
                    del_url: 'bdl/bd/wxuser/del',
                    multi_url: 'bdl/bd/wxuser/multi',
                    export_url: 'bdl/bd/wxuser/export',
                    table: 'bd_user',
                }
            });

            var table       = $("#table");
            var showExport  = $(".btn-export").data('value')

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                showToggle: false,
                showExport: showExport,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID',operate:false},
                        {
                            field: 'forgid', title: __('数据中心/组织'), searchList: function (column) {
                                return Template('forgidtpl', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'fenable', title: '使用状态', searchList: {"1":'可用',"0":'禁用'}, formatter: Table.api.formatter.status}  ,
                        {field: 'fnumber', title: '编码', operate: 'LIKE'},

                        {field: 'datacenter.fname', title: '数据中心名称',operate: false},
                        {field: 'forg.fname', title: '组织名称',operate: false},
                        {field: 'forg.fnumber', title: '组织编码',operate: false},
                        {field: 'forg.id', title: '组织ID',operate: false},

                        {field: 'customer.fname', title: '客户', operate: 'LIKE'},

                        {field: 'fk_bdl_truename', title: '姓名', operate: 'LIKE'},
                        {field: 'fk_bdl_phone', title: '手机号码', operate: 'LIKE'},
                        {field: 'fk_bdl_usertype', title: '人员类型', searchList: {"1":'客户',"2":'业务员',"3":'其他'}, formatter: Table.api.formatter.status}  ,

                        {field: 'fk_bdl_openid', title: '微信用户唯一标识', operate: 'LIKE'},
                        {field: 'fk_bdl_reqcompanyname', title: '注册公司名称', operate: 'LIKE'},
                        {field: 'fk_bdl_requsername', title: '注册联系人', operate: 'LIKE'},
                        {field: 'fk_bdl_reqaddress', title: '注册地址', operate: 'LIKE'},
                        {field: 'fk_bdl_reqattachurl', title: '注册营业执照', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},



                        {field: 'fk_bdl_comment', title: '备注', operate: 'LIKE'},
                        {field: 'fk_bdl_creatorid', title: '创建人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                        {field: 'creator.nickname', title: '创建人', operate: false},
                        {field: 'fk_bdl_createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_fmodifierid', title: '修改人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                        {field: 'modifier.nickname', title: '修改人', operate: false},
                        {field: 'fk_bdl_modifytime', title: '修改时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'bdl/bd/wxuser/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'fenable', title: '使用状态', searchList: {"1":'可用',"0":'禁用'}, formatter: Table.api.formatter.status}  ,
                        {field: 'fnumber', title: '编码', operate: 'LIKE'},

                        {field: 'fk_bdl_bd_datacenter', title: '数据中心ID',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_datacenter_list")},
                        {field: 'datacenter.fname', title: '数据中心名称',operate: false},

                        {field: 'forgid', title: '组织ID',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_org_list")},
                        {field: 'forg.fname', title: '组织名称',operate: false},


                        {field: 'fk_bdl_truename', title: '姓名', operate: 'LIKE'},
                        {field: 'fk_bdl_phone', title: '手机号码', operate: 'LIKE'},
                        {field: 'fk_bdl_phone', title: '邮箱', operate: 'LIKE'},
                        {field: 'fk_bdl_gender', title: '性别', searchList: {"1":'男',"0":'女'}, formatter: Table.api.formatter.status}  ,
                        {field: 'fk_bdl_usertype', title: '人员类型', searchList: {"1":'客户',"2":'业务员',"3":'其他'}, formatter: Table.api.formatter.status}  ,

                        {field: 'fk_bdl_openid', title: '微信用户唯一标识', operate: 'LIKE'},
                        {field: 'fk_bdl_reqcompanyname', title: '注册公司名称', operate: 'LIKE'},
                        {field: 'fk_bdl_requsername', title: '注册联系人', operate: 'LIKE'},
                        {field: 'fk_bdl_reqaddress', title: '注册地址', operate: 'LIKE'},
                        {
                            field: 'fk_bdl_deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'bdl/bd/wxuser/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'bdl/bd/wxuser/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function ()
        {
            $(document).ready(function(){
                $(document).on('change','#forgid',function(){
                    var that = $(this)
                    var val  = that.val()
                    layer.load(1);

                    $.ajax({
                        url:'bdl/bd/datacenter/getFilter?type=get_customer_list_for_form_select',
                        data:{forgid:val},
                        success:function (res)
                        {
                            if(res.code == 1)
                            {
                                var str = `<select  id="c-fk_bdl_bd_customer" required class="form-control selectpicker"  data-live-search="true"  name="row[fk_bdl_bd_customer]">`


                                for (i=0;i<res.data.length;i++)
                                {
                                    str += `<option value="${res.data[i]['value']}" >${res.data[i]['name']}</option>`
                                }
                                str +=  `</select>`

                                $(".fk_bdl_bd_customer").html(str)

                                Form.api.bindevent($(".fk_bdl_bd_customer"));
                            }else{
                                layer.alert(res.msg)
                            }

                            layer.closeAll('loading');

                        }
                    })


                })
            })
            Controller.api.bindevent();
        },
        edit: function ()
        {
            $(document).ready(function()
            {
                // 初始化计数器
                let selectChangeCount = 0;


                $(document).on('change','#forgid',function()
                {
                    selectChangeCount++;

                    var that = $(this)
                    var val  = that.val()

                    // 客户
                    var cust      = $("#cus").val()

                    if(val !== '')
                    {
                        if(selectChangeCount > 1 || cust == 2)
                        {

                            layer.load(1);

                            $.ajax({
                                url:'bdl/bd/datacenter/getFilter?type=get_customer_list_for_form_select',
                                data:{forgid:val},
                                success:function (res)
                                {
                                    if(res.code == 1)
                                    {
                                        var str = `<select  id="c-fk_bdl_bd_customer" required class="form-control selectpicker"  data-live-search="true"  name="row[fk_bdl_bd_customer]">`


                                        for (i=0;i<res.data.length;i++)
                                        {
                                            str += `<option value="${res.data[i]['value']}" >${res.data[i]['name']}</option>`
                                        }
                                        str +=  `</select>`

                                        $(".fk_bdl_bd_customer").html(str)

                                        Form.api.bindevent($(".fk_bdl_bd_customer"));
                                    }else{
                                        layer.alert(res.msg)
                                    }

                                    layer.closeAll('loading');

                                }
                            })
                        }
                    }




                })
            })

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});
